#include "bearpi_h3863.h"

bearpi_h3863::bearpi_h3863(const std::string& work_mode)
{
    trans_mode = new transparent_mode("/dev/ttyUSB0");

    if(trans_mode != nullptr)
    {
        trans_mode->start();
        //read_thread = std::thread(&bearpi_h3863::read_thread_handler, this);    //启动服务端线程
    }

}

bearpi_h3863::~bearpi_h3863()
{
}

//数据读取线程
void bearpi_h3863::read_thread_handler()
{
    bool running = true;
    static int count = 0;
    unsigned char data_buff[128];
    int read_bytes = 0;

    while(running)
    {
        read_bytes = trans_mode->read_bytes_poll(data_buff, 128);

        if(read_bytes > 0)
        {
            count += read_bytes;

            std::cout << "all data size : " << count << " ";
            //处理这些数据
            std::cout << "proccessing data: ";
            for(int i = 0; i < read_bytes; i++)
            {
                std::cout << std::hex << static_cast<int>(data_buff[i]) << " ";
            }
            std::cout << std::endl;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
}  


int bearpi_h3863::transmode_read_bytes_pool(unsigned char *p_data, int length)
{
    int read_bytes = 0;

    if(length > 0)
    {
        read_bytes = trans_mode->read_bytes_poll(p_data, length);
    }

    return read_bytes;    
}

int bearpi_h3863::transmode_send_bytes_data(unsigned char *p_data, int length)
{
    if(trans_mode == nullptr)return -1;

    if((p_data != nullptr) && (length > 0))
    {
        trans_mode->send_bytes_data(p_data, length);
        return length;
    }
    
    return 0;
}

